package com.example.szh.mvp.view;

import android.graphics.Color;
import android.view.View;

import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.example.szh.R;
import com.example.szh.fragment.LiveFragment;
import com.example.szh.fragment.NewsFragment;
import com.example.szh.fragment.PeppleNmFragment;
import com.example.szh.fragment.UserFragment;
import com.example.szh.fragment.VideoFragment;
import com.example.szh.mvp.contract.FragmContract;
import com.example.szh.mvp.di.DaggerFragmComponent;
import com.example.szh.mvp.di.FragmModels;
import com.example.szh.mvp.presenter.FragmPresenter;
import com.example.szh.nextwork.view.BaseActivity;
import com.example.szh.widget.BottomButton;

import java.util.Map;

import constant.DownLoadBy;
import model.UpdateConfig;
import update.UpdateAppUtils;

public class FragmentActivity extends BaseActivity<FragmPresenter> implements BottomButton.SelectListenter, FragmContract.IFragmView {

    private BottomButton bntNews;
    private BottomButton bntPeopleNum;
    private BottomButton bntVideo;
    private BottomButton bntLive;
    private BottomButton bntMy;
    private NewsFragment newsFragment;
    private PeppleNmFragment peppleNmFragment;
    private VideoFragment videoFragment;
    private LiveFragment liveFragment;
    private UserFragment userFragment;

    @Override
    public View addStatusView() {
        return f(R.id.frame_linear);
    }

    @Override
    public void refresh(Map<String, Object> resultMap) {

    }

    @Override
    public int bindLayout() {
        return R.layout.activity_my;
    }

    @Override
    public void initView() {



        bntNews = (BottomButton) findViewById(R.id.bnt_news);
        bntPeopleNum = (BottomButton) findViewById(R.id.bnt_peopleNum);
        bntVideo = (BottomButton) findViewById(R.id.bnt_video);
        bntLive = (BottomButton) findViewById(R.id.bnt_live);
        bntMy = (BottomButton) findViewById(R.id.bnt_my);
        bntNews.setContent("新闻").setNomalColor(Color.BLACK).setSelColor(Color.RED).setSelImg(R.mipmap.tu1_t)
                .setNomalImg(R.mipmap.tu1_f).setListenter(this::onSelect).setShowPoint(true);
        bntPeopleNum.setContent("人民号").setNomalColor(Color.BLACK).setSelColor(Color.RED).setSelImg(R.mipmap.tu2_t)
                .setNomalImg(R.mipmap.tu2_f).setListenter(this::onSelect).setShowPoint(false);
        bntVideo.setContent("视频").setNomalColor(Color.BLACK).setSelColor(Color.RED).setSelImg(R.mipmap.tu3_t)
                .setNomalImg(R.mipmap.tu3_f).setListenter(this::onSelect).setShowPoint(false);
        bntLive.setContent("直播").setNomalColor(Color.BLACK).setSelColor(Color.RED).setSelImg(R.mipmap.tu4_t)
                .setNomalImg(R.mipmap.tu4_f).setListenter(this::onSelect).setShowPoint(false);
        bntMy.setContent("我的").setNomalColor(Color.BLACK).setSelColor(Color.RED).setSelImg(R.mipmap.tu5_t)
                .setNomalImg(R.mipmap.tu5_f).setListenter(this::onSelect).setShowPoint(false);
    }

    @Override
    public void inject() {
        DaggerFragmComponent.builder().fragmModels(new FragmModels(this)).build().inject(this);
    }

    @Override
    public void initData() {
        UpdateConfig config=new UpdateConfig(true,true,
                false,false,false
                , "","apk.1.1"
                , DownLoadBy.APP
                ,false,true
                ,0
                ,false,true,
                "",2);
        UpdateAppUtils.getInstance().apkUrl("upDataEntity.getUrl()")
                .updateTitle("更新版本")
                .updateContent("更新新版本？要不然你走开~~~")
                .updateConfig(config)
                .update();
//
//        p.requestUpdata();
        if (newsFragment==null){
            newsFragment= new NewsFragment();
        }

        FragmentManager supportFragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.ll, newsFragment);
        fragmentTransaction.show(newsFragment);
        fragmentTransaction.commit();

    }

    @Override
    public void onSelect(int id) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        if (newsFragment != null) {
            fragmentTransaction.hide(newsFragment);
        }
        if (peppleNmFragment != null) {
            fragmentTransaction.hide(peppleNmFragment);
        }
        if (videoFragment != null) {
            fragmentTransaction.hide(videoFragment);
        }
        if (liveFragment != null) {
            fragmentTransaction.hide(liveFragment);
        }
        if (userFragment != null) {
            fragmentTransaction.hide(userFragment);
        }
        if (id==R.id.bnt_news){
            bntLive.clearSelect();
            bntMy.clearSelect();
            bntPeopleNum.clearSelect();
            bntVideo.clearSelect();
            fragmentTransaction.show(newsFragment);
            getStatusColor(false);
        }else if (id==R.id.bnt_peopleNum){
            bntLive.clearSelect();
            bntMy.clearSelect();
            bntNews.clearSelect();
            bntVideo.clearSelect();
            if (peppleNmFragment == null) {
                peppleNmFragment = new PeppleNmFragment();
                fragmentTransaction.add(R.id.ll, peppleNmFragment);
            } else {
                fragmentTransaction.show(peppleNmFragment);
            }
            getStatusColor(false);
        }else if (id==R.id.bnt_video){
            bntLive.clearSelect();
            bntMy.clearSelect();
            bntPeopleNum.clearSelect();
            bntNews.clearSelect();
            if (videoFragment == null) {
                videoFragment = new VideoFragment();
                fragmentTransaction.add(R.id.ll, videoFragment);
            } else {
                fragmentTransaction.show(videoFragment);
            }
            getStatusColor(false);
        }else if (id==R.id.bnt_live){
            bntNews.clearSelect();
            bntMy.clearSelect();
            bntPeopleNum.clearSelect();
            bntVideo.clearSelect();
            if (liveFragment == null) {
                liveFragment = new LiveFragment();
                fragmentTransaction.add(R.id.ll, liveFragment);
            } else {
                fragmentTransaction.show(liveFragment);
            }
            getStatusColor(false);
        }else if (id==R.id.bnt_my){
            getStatusColor(true);
            bntLive.clearSelect();
            bntNews.clearSelect();
            bntPeopleNum.clearSelect();
            bntVideo.clearSelect();
            if (userFragment == null) {
                userFragment = new UserFragment();
                fragmentTransaction.add(R.id.ll, userFragment);
            } else {
                fragmentTransaction.show(userFragment);
            }
        }
        fragmentTransaction.commit();
    }
}